﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using Tourreau.Gilles.FaxDotNet;
using System.IO;
using Spring.Context;
using Spring.Context.Support;
using FaxServerNET.Email;
using log4net.Config;

namespace FaxServerNET
{
    class Program
    {
        //TODO find out how to read emails and send their attachments via fax. 
        // also send emails when fax is received.
        // also documents are being shown blank in the viewer maybe not processing the PDF?
        static void Main2(string[] args)
        {
            Console.WriteLine(System.IO.Directory.GetCurrentDirectory());

           // SendFax("Juanito The Recipent", "+3055957519", @"C:\MyProjects\FaxServerNET\FaxServerNET\Exams_1st_Sem.pdf");
            //SendFax("Juanito The Recipent", "+3055957519", @"C:\MyProjects\FaxServerNET\FaxServerNET\campus2.bmp");

        }

        static void Main(string[] args)
        {
            XmlConfigurator.Configure();



            #region Service install parameters parsing

            if ((args.GetLength(0) >= 1) && (args[0] == "-install"))
            {
                if (args.Length == 2)
                {
                    if (args[1][0] == '-')
                        args[1] = args[1].Replace('-', '/');

                    string path = typeof(System.Object).Assembly.Location;
                    StringBuilder sb = new StringBuilder(path.Substring(0, path.LastIndexOf("\\")));
                    sb.Append("\\InstallUtil.exe");
                    AppDomain.CreateDomain("Service Installer").ExecuteAssembly(sb.ToString(), new string[] { args[1], typeof(Program).Assembly.ManifestModule.Name });
                }
                else
                {
                    Console.WriteLine("Usage: -install -i or -Install -u\n");
                }
                return;
            }

            #endregion

            else if ((args.GetLength(0) >= 1) && (args[0] == "-console"))
            {
                IApplicationContext context = new XmlApplicationContext("configuration\\applicationContext.XML");

                FaxCore fax_core = (FaxCore) context.GetObject("fax_core");


                fax_core.RunServer();
                Console.Read();
            }
            else
            { // Arranque em modo serviço (invocado pelo Service Manager)
                ServiceBase[] ServicesToRun = new ServiceBase[] { new FaxServerService() };

                ServiceBase.Run(ServicesToRun);
            }
        }

    }
}
